export default {
  name: 'Rights',
  data () {
    return {
      rolesList: [],
      addRolesDialog: false,
      addRolesInfo: {},
      addRolesRules: {
        roleName: [
          { required: true, message: '请填写角色名称', trigger: 'change' }
        ],
        roleDesc: [
          { required: true, message: '请填写角色信息', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data } = await this.$get('roles')
      console.log(data)
      this.rolesList = data
    },
    closeAddRolesDialog () {
      this.$refs.addRoles.resetFields()
      this.addRolesInfo = {}
    },
    addRolesHandler () {
      this.$refs.addRoles.validate(async valid => {
        if (!valid) {
          this.$message({
            type: 'error',
            message: '请按照规则填写表单'
          })
          return
        }
        const { data, meta } = await this.$post('roles', this.addRolesInfo)
        console.log(data)
        console.log(meta)
        if (meta.status === 201) {
          this.$message({
            type: 'success',
            message: '创建角色成功'
          })
          this.getRolesList()
          this.addRolesDialog = false
        }
      })
    },
    deleteRolesHandler (id) {
      this.$confirm('你真的要删除该角色吗？', '删除角色', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { meta } = await this.$delete(`roles/${id}`)
        if (meta.status === 200) {
          this.$message({
            type: 'success',
            message: '删除该角色成功'
          })
          this.getRolesList()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
